﻿using Atlanchim.Crm.Mui.Commands;
using Atlanchim.Crm.Mui.Helpers;
using FirstFloor.ModernUI.Windows.Controls;
using Lmondeil.Crm.EF;
using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;

namespace Atlanchim.Crm.Mui.Pages.Emails
{
    public class PageViewEmailModel : AbstractPageViewModel
    {
        /// <summary>
        /// Contact de référence
        /// </summary>
        public Email Email { get; set; }

        /// <summary>
        /// Url d'accès à la page d'édition
        /// </summary>
        public override string EditUrl { get; set; }

        /// <summary>
        /// Chargement
        /// </summary>
        public override void Load()
        {
            // Lecture du fragment
            int idEmail = 0;
            int.TryParse(this.DicFragments["IdEmail"], out idEmail);

            // Chargement
            using (CrmContext context = new CrmContext())
            {
                // Contact
                this.Email = context
                                .Emails
                                .Where(tel => tel.Id == idEmail)
                                .FirstOrDefault();
                this.EditUrl = string.Format("/Pages/Emails/PageEditEmail.xaml#IdEmail={0}", idEmail);
            }

            // Notification
            OnPropertyChanged("Email");
        }

        /// <summary>
        /// Suppression
        /// </summary>
        protected override void OnDeleteCurrentEntity()
        {
            CrmCrudHelper.DeleteEntityNewContext(this.Email);
        }
    }
}
